<?php

class AuthController extends BaseController {

	/*
	|--------------------------------------------------------------------------
	| Default Home Controller
	|--------------------------------------------------------------------------
	|
	| You may wish to use controllers instead of, or in addition to, Closure
	| based routes. That's great! Here is an example controller method to
	| get you started. To route to this controller, just add the route:
	|
	|	Route::get('/', 'HomeController@showWelcome');
	|
	*/

	public function getLogin(){
		return View::make('auth.login');
	}

	public function postLogin()
	{
		$usernameinput = Input::get('username');
		$password = Input::get('password');
		$field = filter_var($usernameinput, FILTER_VALIDATE_EMAIL) ? 'email' : 'phone';

		if(Input::get('remember_me') == 'on') {
			if (Auth::attempt(array($field => $usernameinput, 'password' => $password, 'active' => 1), true)) {
				return Redirect::to('/');
			}
		}
		else {
			if (Auth::attempt(array($field => $usernameinput, 'password' => $password, 'active' => 1))) {
				return Redirect::to('/');
			}
		}
		
		return Redirect::back()->with('error','用户不存在或密码错误');
	}

	public function getLogout(){
		Auth::logout();
		return Redirect::to('account/login')->with('message', "已经安全退出");
	}

    public function getActivate($code)
    {
        $user = User::whereToken($code)->whereActive(0);

        if($user->count()) {
            $user = $user->first();

            $user->active = 1;
            $user->token = '';

            if($user->save()) {
                return Redirect::guest('account/login')->with('message', '你的账户已激活，可以登陆');
            }
        }

        return Redirect::guest('account/login')->with('error', '对不起我们无法激活你的账户，请稍后再试');
    }
}
		